package com.dao;

import com.entity.game;
import com.entity.product;
import com.entity.productdetails;
import com.entity.user;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author 刘锦华
 */
public class searchdaoimpl implements searchdao {
    @Override
    public List<productdetails> search(String message) {
        UserInfDao u = new UserInfDao();
        List<productdetails> list = new ArrayList<>();
        String sql="select * from product where Audit='1'";
        List<product> list6=u.getForList(product.class,sql);
        for (int i = 0; i < list6.size(); i++) {
            String pid=list6.get(i).getProductID();
            String sql1 = "select * from productdetails where Zone like CONCAT('%',?,'%') && ProductID=?";
//        String sql2 = "select * from productdetails where Level like CONCAT('%',?,'%')";
            String sql3 = "select * from productdetails where PDID in (" +
                    "select PDID from product where GameID in (" +
                    "select GameID from game where GameName like CONCAT('%',?,'%')) && ProductID=?" +
                    ")";
            String sql4 = "select * from productdetails where UnitPrice like CONCAT('%',?,'%') && ProductID=?";
            String sql5 = "select * from productdetails where ProductValue like CONCAT('%',?,'%') && ProductID=?";
            List<productdetails> list1 = u.getForList(productdetails.class, sql1, message,pid);
            List<productdetails> list3 = u.getForList(productdetails.class, sql3, message,pid);
            List<productdetails> list4 = u.getForList(productdetails.class, sql4, message,pid);
            List<productdetails> list5 = u.getForList(productdetails.class, sql5, message,pid);

            for (com.entity.productdetails productdetails : list5) {
                assert false;
                list.add(productdetails);
            }

            for (com.entity.productdetails productdetails : list4) {
                assert false;
                list.add(productdetails);
            }

            for (com.entity.productdetails productdetails : list3) {
                assert false;
                list.add(productdetails);
            }

            for (com.entity.productdetails productdetails : list1) {
                assert false;
                list.add(productdetails);
            }
        }
        return list;
    }

    @Override
    public List<productdetails> searchs(String message, String message2,String message3) {
        judge j = new judgeimpl();
        UserInfDao u = new UserInfDao();
        String id = "";
        String id1 = "";
        List<productdetails> list = new ArrayList<>();
        if("".equals(message))
        {
            searchdao s=new searchdaoimpl();
            Set<productdetails> list5 = new HashSet<>(s.search(message2));
            if(!"".equals(message3)) {
                for (com.entity.productdetails productdetails : list5) {
                    if (message3.equals(productdetails.getZone())) {
                        list.add(productdetails);
                    }
                }
            }
            else {
                for (com.entity.productdetails productdetails : list5) {
                    assert false;
                    list.add(productdetails);
                }
            }
        }
        else if(!"".equals(message)&&"".equals(message2))
        {
            String sql2 = "select * from game where GameName =?";
            String sql3 = "select * from game where FatherGameID=?";
            List<game> g = u.getForList(game.class, sql2, message);
            for (int i = 0; i < g.size(); i++) {
                id = g.get(i).getGameID();
            }
            List<game> g1 = u.getForList(game.class, sql3, id);
            for (int i = 0; i < g1.size(); i++) {
                id1 = g1.get(i).getGameID();
                String sql4 = "select * from productdetails where PDID in (" +
                        "select PDID from product where GameID =?)";
                List<productdetails> list4 = u.getForList(productdetails.class, sql4, id1);
                if(!"".equals(message3)) {
                    for (com.entity.productdetails productdetails : list4) {
                        if (message3.equals(productdetails.getZone())) {
                            list.add(productdetails);
                        }
                    }
                }
                else {
                    for (com.entity.productdetails productdetails : list4) {
                        assert false;
                        list.add(productdetails);
                    }
                }
            }
        }
        boolean b = j.judgeisonshelve(message, message2, message3);
        if (b) {
            String sql2 = "select * from game where GameName =?";
            String sql3 = "select * from game where FatherGameID=?";
            List<game> g = u.getForList(game.class, sql2, message);
            for (int i = 0; i < g.size(); i++) {
                id = g.get(i).getGameID();
            }
            List<game> g1 = u.getForList(game.class, sql3, id);
            for (int i = 0; i < g1.size(); i++) {
                if (message2.equals(g1.get(i).getGameName())) {
                    id1 = g1.get(i).getGameID();
                }
            }
            String sql4 = "select * from productdetails where PDID in (" +
                    "select PDID from product where GameID =?)";
            List<productdetails> list4 = u.getForList(productdetails.class, sql4, id1);
            if(!"".equals(message3)) {
            for (com.entity.productdetails productdetails : list4) {
                if (message3.equals(productdetails.getZone())) {
                    list.add(productdetails);
                }
            }
            }
            else {
                for (com.entity.productdetails productdetails : list4) {
                    assert false;
                    list.add(productdetails);
                }
            }
        }
            return list;
        }

    @Override
    public String searchname(String id) {
        String i="";
        String sql1="select * from product where PDID=?";
        UserInfDao u=new UserInfDao();
        List<product> list1=u.getForList(product.class,sql1,id);
        for (int j = 0; j < list1.size(); j++) {
            String s=list1.get(j).getGameID();
            String sql2="select * from game where GameID=?";
            List<game> list2=u.getForList(game.class,sql2,s);
            for (int k = 0; k < list2.size(); k++) {
                i=i.concat(list2.get(k).getGameName());
            }
        }
        return i;
    }

    @Override
    public String searchfname(String id) {
        String i="";
        String n="";
        String sql1="select * from product where PDID=?";
        UserInfDao u=new UserInfDao();
        List<product> list1=u.getForList(product.class,sql1,id);
        for (int j = 0; j < list1.size(); j++) {
            String s=list1.get(j).getGameID();
            String sql2="select * from game where GameID=?";
            List<game> list2=u.getForList(game.class,sql2,s);
            for (int k = 0; k < list2.size(); k++) {
                n=n.concat(list2.get(k).getGameName());
            }
        }
        String sql2="select * from game where GameName=?";
        List<game> list2=u.getForList(game.class,sql2,n);
        for (int j = 0; j < list2.size(); j++) {
            String s1=list2.get(j).getFatherGameID();
            String sql3="select * from game where GameID=?";
            List<game> list3=u.getForList(game.class,sql3,s1);
            for (int k = 0; k < list3.size(); k++) {
                i=i.concat(list3.get(k).getGameName());
            }
        }
        return i;
    }

    @Override
    public String searchUser(String id) {
        String i="";
        String sql1="select * from product where PDID=?";
        UserInfDao u=new UserInfDao();
        List<product> list =u.getForList(product.class,sql1,id);
        i=i.concat(list.get(0).getUserID());
        return i;
    }

    @Override
    public String searchUsername(String id) {
        String i="";
        String sql="select * from user where UserID=?";
        UserInfDao u=new UserInfDao();
        List<user> list=u.getForList(user.class,sql,id);
        i=i.concat(list.get(0).getUserName());
        return i;
    }

    @Override
    public String searchmoney(String user) {
        String i="";
        String sql1="select * from user where UserID=?";
        UserInfDao u=new UserInfDao();
        List<user> list1=u.getForList(com.entity.user.class,sql1,user);
        i=i.concat(list1.get(0).getMoney());
        return i;
    }

    @Override
    public String searchcookie(HttpServletRequest req) {
        Cookie[] cookies=req.getCookies();
        String name="";
        if(cookies!=null)
        {
            for (int i = 0; i < cookies.length; i++) {
                Cookie c=cookies[i];
                if("dlid".equals(c.getName()))
                {
                    name=name.concat(c.getValue());
                }
            }
        }
        return name;
    }
    @Override
    public String searchcookiee(HttpServletRequest req) {
        Cookie[] cookies=req.getCookies();
        String name="";
        if(cookies!=null)
        {
            for (int i = 0; i < cookies.length; i++) {
                Cookie c=cookies[i];
                if("yemian".equals(c.getName()))
                {
                    name=name.concat(c.getValue());
                }
            }
        }
        return name;
    }

    @Override
    public String searchshcookie(HttpServletRequest req) {
        Cookie[] cookies=req.getCookies();
        String name="";
        if(cookies!=null)
        {
            for (int i = 0; i < cookies.length; i++) {
                Cookie c=cookies[i];
                if("shenhe".equals(c.getName()))
                {
                    name=name.concat(c.getValue());
                }
            }
        }
        return name;
    }

    @Override
    public String searchsjcookie(HttpServletRequest req) {
        Cookie[] cookies=req.getCookies();
        String name="";
        if(cookies!=null)
        {
            for (int i = 0; i < cookies.length; i++) {
                Cookie c=cookies[i];
                if("shijian".equals(c.getName()))
                {
                    name=name.concat(c.getValue());
                }
            }
        }
        return name;
    }

    @Override
    public String searchglcookie(HttpServletRequest req) {
        Cookie[] cookies=req.getCookies();
        String name="";
        if(cookies!=null)
        {
            for (int i = 0; i < cookies.length; i++) {
                Cookie c=cookies[i];
                if("gldl".equals(c.getName()))
                {
                    name=name.concat(c.getValue());
                }
            }
        }
        return name;
    }
}